package easypoi;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.Hutool;
import cn.hutool.core.util.RandomUtil;
import com.ms.education.easypoi.EasyPoiApplication;
import com.ms.education.easypoi.entity.*;
import com.ms.education.easypoi.mapper.UserMapper;
import com.ms.education.easypoi.service.*;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * <p>Project:education-test
 * <p>Powered by ms on 2023-01-12 18:55:12
 *
 * @author ms
 * @version 1.0
 * @since 1.8
 */
@SpringBootTest(classes = EasyPoiApplication.class)
public class test {

    @Autowired
    private UserService userService;

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private StatisticsDailyService statisticsDailyService;

    String[] careers = {"教授", "副教授", "讲师", "兼聘讲师"};
    Integer[] levels = {1, 2};
    Integer[] sorts = {1, 2, 3, 4, 5, 6};
    String[] intros = {"高级讲师简历", "普通讲师简历"};
    String[] avatars = {"https://gss0.baidu.com/-fo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/cefc1e178a82b901540b2b9f7b8da9773812effc.jpg",
            "https://gss0.baidu.com/9vo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/f31fbe096b63f6240a36d2758f44ebf81a4ca359.jpg",
            "https://ss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/zhidao/wh%3D600%2C800/sign=bce662e55bafa40f3c93c6db9b542f77/bd3eb13533fa828b69084834fc1f4134970a5a3a.jpg",
            "https://plc.jj20.com/up/allimg/mx09/10191Z22207/19101Z22207-0.jpg",
            "https://pics0.baidu.com/feed/838ba61ea8d3fd1f016ff7a456ce881894ca5f55.jpeg?token=329039b1041bca57f3cb0c1e6cd73990",
            "https://img2.baidu.com/it/u=2445649548,2936863453&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=739",
            "https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/zhidao/pic/item/d8f9d72a6059252dd1e064b4349b033b5ab5b98e.jpg",
            "https://img.tuke8.com/2022-08/919e51d5b0fccd29d0ec5f77a6817e63.jpg",
            "https://img0.baidu.com/it/u=1339811457,2801482176&fm=253&fmt=auto&app=138&f=JPEG?w=267&h=500",
            "https://5b0988e595225.cdn.sohucs.com/q_70,c_zoom,w_640/images/20190529/796bde3615724aecb1b4f784ac3759e3.jpeg"
    };

    @Test
    public void test01() throws Exception {
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        File file = new File("D:\\桌面\\student.xlsx");
        FileInputStream inputStream = new FileInputStream(file);
        List<User> users = ExcelImportUtil.importExcel(inputStream, User.class, params);
        List<Teacher> teachers = users.stream().map(user -> {
            Teacher teacher = new Teacher();
            teacher.setName(user.getNickname());
            teacher.setAvatar(avatars[RandomUtil.randomInt(0, avatars.length)]);
            teacher.setIntro(intros[RandomUtil.randomInt(0, intros.length)]);
            teacher.setCareer(careers[RandomUtil.randomInt(0, careers.length)]);
            teacher.setLevel(levels[RandomUtil.randomInt(0, levels.length)]);
            teacher.setSort(sorts[RandomUtil.randomInt(0, sorts.length)]);
            return teacher;
        }).collect(Collectors.toList());
        boolean b = teacherService.saveBatch(teachers);
        System.out.println("================" + b);

    }

    @Test
    public void test02() throws Exception {
        RandomUtil.randomInt(0, 10);
        for (int i = 0; i < 200; i++) {
            System.out.println(RandomUtil.randomInt(0, 11));
        }
    }

    //导入课程分类信息
    @Test
    public void test03() throws Exception {
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        File file = new File("D:\\桌面\\edu_subject.xlsx");
        FileInputStream inputStream = new FileInputStream(file);
        List<Subject> subjects = ExcelImportUtil.importExcel(inputStream, Subject.class, params);
        Integer temp;
        for (Subject subject : subjects) {
            if (subject.getParentId() == 0) {
                temp = subject.getId();
                subject.setId(null);
                subjectService.save(subject);
                for (Subject s : subjects) {
                    if (Objects.equals(s.getParentId(), temp)) {
                        s.setParentId(subject.getId());
                        s.setId(null);
                        subjectService.save(s);
                    }
                }
            }
        }
    }

    @Test
    public void test04() throws Exception {
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        File file = new File("D:\\桌面\\statistics_daily.xlsx");
        FileInputStream inputStream = new FileInputStream(file);
        List<StatisticsDaily> statisticsDailies = ExcelImportUtil.importExcel(inputStream, StatisticsDaily.class, params);
        statisticsDailyService.saveBatch(statisticsDailies);

    }

    //导入权限信息
    @Test
    public void test05() throws Exception {
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        File file = new File("D:\\桌面\\acl_permission.xlsx");
        FileInputStream inputStream = new FileInputStream(file);
        List<Permission> permissions = ExcelImportUtil.importExcel(inputStream, Permission.class, params);
//        List<Permission> firstLevelPer = permissions.stream().filter(p -> p.getPid() == 0).collect(Collectors.toList());
//        firstLevelPer.forEach(p -> findPerChildren(permissions, p));

        permissions.forEach(permission -> {
            if (String.valueOf(permission.getPid()).equals("0")) {
                savePermission(permission, permissions);
            }
        });

    }

    public void savePermission(Permission p, List<Permission> permissions) {
        Long temp = p.getId();
        p.setId(null);
        permissionService.save(p);
        for (Permission permission : permissions) {
            if (String.valueOf(permission.getPid()).equals(String.valueOf(temp))) {
                permission.setPid(p.getId());
                savePermission(permission, permissions);
            }
        }
    }

    public void findPerChildren(List<Permission> permissions, Permission parPermission) {
        List<Permission> children = new LinkedList<>();
        for (Permission p : permissions) {
            if (String.valueOf(p.getPid()).equals(String.valueOf(parPermission.getId()))) {
                children.add(p);
                findPerChildren(permissions, p);
            }
        }
        //parPermission.setChildren(children);
        System.out.println("---" + children);
    }


}
